<template>
  <div class="app-container">
    <el-card>
      <el-form inline :model="searchForm">
        <el-form-item label="标题">
          <el-input v-model="searchForm.title" placeholder="请输入标题" size="small" clearable></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.enableStatus" placeholder="请选择状态" clearable size="small">
            <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="发布平台">
          <el-select v-model="searchForm.platformType" placeholder="请选择发布平台" clearable size="small">
            <el-option v-for="item in platformTypeList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="分类">
          <el-select
            v-model="searchForm.codeId"
            placeholder="请选择分类"
            clearable
            size="small"
          >
            <el-option
              v-for="item in cateList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="small" icon="el-icon-refresh" @click="resetForm('searchForm')">重置</el-button>
          <el-button size="small" type="primary" icon="el-icon-search" @click="search()">查询</el-button></el-form-item>
      </el-form>

      <el-button icon="el-icon-plus" size="small" type="primary" @click="addKnowledge()">新增惠懂你</el-button>
      <el-table :data="tableData.data" v-loading="listLoading" border style="width: 100%; margin-top: 20px">
        <el-table-column align="center" label="Id" prop="id" width="80"></el-table-column>
        <el-table-column align="center" label="分类" prop="cateName"></el-table-column>
        <el-table-column align="center" label="标题" prop="title"></el-table-column>
        <el-table-column align="center" label="发布平台" prop="platformType"></el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.enableStatus" :active-value="1" :inactive-value="0"
              @change="updateStatus(scope.row.id)" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="发布时间" prop="createTime"></el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="Detail(scope.row.id)">详情</el-button>
            <el-button size="mini" type="text" :disabled="scope.row.enableStatus ==1" @click="editToolLoans(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="text" :disabled="scope.row.enableStatus ==1" @click="deleteToolLoans(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="pageRight" background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 40]" :current-page="searchForm.pageNum" :page-size="searchForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="tableData.total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import {
  getToolLoansList,
  deleteToolLoans,
  updateToolLoansStatus,
} from "@/api/tool";
import { getDictLabel } from "@/utils/dict";
export default {
  data() {
    return {
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        title: "",
        enableStatus: "",
        platformType: "",
        toolType: "hui",
        codeId: ""
      },
      platformTypeList: [],
      statusList: [
        { label: "启用", value: 1 },
        { label: "禁用", value: 0 },
      ],
      tableData: {
        data: [],
        total: 0,
      },
      listLoading: false,
      cateList: [],
    };
  },
  async created() {
    this.platformTypeList = await this.$getDictData("platform_type");
    this.cateList = await this.$getDictData("hui");
    this.getList();
  },
  methods: {
    updateStatus(id) {
      updateToolLoansStatus(id).then((res) => {
        this.$message({
          message: "修改成功",
          type: "success",
        });
        this.getList();
      });
    },
    deleteToolLoans(id) {
      this.$confirm("是否删除该数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteToolLoans(id).then((res) => {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getList();
        });
      });
    },
    Detail(id) {
      this.$router.push({
        path: "/tool/knowQuestionAdd",
        query: {
          id: id,
          type: "detail",
        },
      });
    
    },
    editToolLoans(id) {
      this.$router.push({
        path: "/tool/knowQuestionAdd",
        query: {
          id: id,
        },
      });
    },
    addKnowledge() {
      this.$router.push({
        path: "/tool/knowQuestionAdd",
      });
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.searchForm.pageNum = val;
      this.getList();
    },
    getList() {
      this.listLoading = true;
      getToolLoansList(this.searchForm)
        .then((res) => {
          res.data.list.forEach((item) => {
            item.platformType = getDictLabel(
              item.platformType,
              this.platformTypeList
            );
            item.cateName = getDictLabel(item.codeId, this.cateList);
          });

          this.tableData.data = res.data.list;
          this.tableData.total = res.data.total;
          this.listLoading = false;
        })
        .finally(() => {
          this.listLoading = false;
        });
    },
    search() {
      this.searchForm.pageNum = 1;
      this.getList();
    },
    resetForm(form) {
      // this.$refs[form].resetFields();
      this.searchForm = {
        pageNum: 1,
        pageSize: 10,
        title: "",
        enableStatus: "",
        toolType: "hui",
      };
      this.getList();
    },
  },
};
</script>

<style></style>
